// pages/share/share.js
const apiurl = require('../../config').apiurl;
import { Pulibc } from '../../models/public.js'
const pulibc = new Pulibc();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
saveImageBool:'' //图片授权
  },
  // 获取传过来的数据
  getData(id){
    var url = apiurl +'/goods/fxgoodsinfo';
    wx.request({
      url: url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: { goods_id:id},
      success:data=>{
        var info = data.data.datas.info;
        console.log(info)
        this.setData({
          info
        })
      }
    })
  },
  // 复制
  copy(){
    var data = this.data.info.goods_title;
    wx.setClipboardData({
      data:data,
      success:data=>{
        console.log(data)
      }
      
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.getData(id)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync("key") == false) {
      wx.navigateTo({
        url: "../register/register"
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var title = this.data.info.goods_name
    var id = this.data.info.goods_id;
      return {
        title: title,
        path:'/pages/details-goods/details-goods?id='+id
      }
  },
  chengPreserve(){
    console.log(this.data.info)
    var goodname = this.data.info.goods_name;
    var goods_id = this.data.info.goods_id; 
    var xianjia = this.data.info.goods_price;
    var src_path = this.data.info.goods_pic
    var user_id = wx.getStorageSync('member_id') ;
    var path = '/pages/details-goods/details-goods?id=' + goods_id
    var url = apiurl +'/goods/doPageCreate';

    console.log(src_path)
    wx.showLoading({
      title: "图片保存中"
    })
    wx.request({
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      url: url,
      method:'POST',
      data:{
        goodname: goodname,
        xianjia: xianjia,
        src_path: src_path,
        user_id: user_id,
        path: path,
        goods_id: goods_id
      
      },
      success:data=>{
        // console.log(data.data)
        this.getPreserve()
      },fail:err=>{
        this.getPreserve()
      }
    })
  },
  getPreserve(){
    let that=this;
    var url = apiurl + '/goods/doPageCreatePoster'
    wx.request({
      url: url,
      method:'POST',
      success:data=>{
        var img = data.data.datas.img;
        console.log(img)
        wx.downloadFile({
          url:img,
          success:data=>{
           
            console.log('tempFilePath :'+data.tempFilePath)
             wx.saveImageToPhotosAlbum({
              filePath: data.tempFilePath,
              success:data=>{
                console.log(data)
                that.setData({
                  saveImageBool: true
                })
                wx.showToast({
                  title: "保存成功",
                  icon: "success",
                  duration: 2e3
                })
              },
              fail:err=>{
                console.log("saveimg",err)
                that.setData({
                  saveImageBool:false
                })
                // 隐藏loading
                wx.hideLoading();
                // fail信息为未授权时，才调用。
                if(err.errMsg.indexOf('auth')!=-1){
                  that.getPromission();
                }
              }
            });
          },
          fail:err=>{
            console.log("err2",err)
          }
        })
      }
    })
  },
  // 用户授权保存到相册
   getPromission() {
    let that = this;
     console.log("saveImageBool", that.data.saveImageBool)
     if (that.data.saveImageBool == false) {
      wx.showModal({
        title: '用户未授权',
        content: '请开启相应权限哦',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.openSetting({
              success: data => {
                console.log("data", data)
              //   console.log(Object.values(data.authSetting))
              // let authSettingArr=Object.values(data.authSetting);
              //   if (authSettingArr.indexOf(false) !== -1){
              //     that.getPreserve();
              //   }else {
              //     that.getPreserve();
              //   }
                if (data.authSetting["scope.writePhotosAlbum"] == true) {
                  that.getPreserve();
                } else {
                  that.getPromission();
                }
              }
            })
          }
        }
      })
    }
  },
})